$(function(){

  $('#form').bootstrapValidator({
    excluded: [':disabled', ':hidden', ':not(:visible)'],
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        username: {
            validators: {
                notEmpty: {
                    message: '用户名不能为空'
                },
                stringLength: {
                    min: 2,
                    max: 6,
                    message: '用户名格式不正确'
                },
                regexp: {
                    regexp: /^[a-zA-Z0-9_]+$/,
                    message: '用户名只能包含大写、小写、数字和下划线'
                },
                callback:{
                    message:'用户名不存在'
                }
            }
        },
        password: {
            validators: {
                notEmpty: {
                    message: '密码不能为空'
                },
                stringLength: {
                  min: 6,
                  max: 12,
                  message: '密码格式不正确'
              },
              regexp: {
                regexp: /^[a-zA-Z0-9_]+$/,
                message: '用户名只能包含大写、小写、数字和下划线'
            },
             callback:{
                 message:'密码不正确'
             }
            }
        }
    }

  })

$('.btn-reset').on('click',function(){
  $('#form').data('bootstrapValidator').resetForm();
})

  $('#form').on('success.form.bv',function(e){
    e.preventDefault();
    $.ajax({
        url:'/employee/employeeLogin',
        type:'post',
        data:$('#form').serialize(),
        dataType:'json',
        success:function(info){
            console.log(info)
            if(info.success){
                location.href = 'index.html'
            }else if(info.error == 1000){
                $('#form').data('bootstrapValidator').updateStatus('username', 'INVALID', 'callback');
            }else{
                $('#form').data('bootstrapValidator').updateStatus('password', 'INVALID', 'callback');
            }
        }
    })
  })

})